//
//  ViewController.m
//  testios
//
//  Created by bigdudu on 16/2/15.
//  Copyright © 2016年 bigdudu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//- (void)loadView{
//    UIView *view = [[UIView alloc]initWithFrame:CGRectZero];
//    self.view = view;
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollView.contentSize = CGSizeMake(320, 600);
    self.scrollView.autoresizingMask =UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight;
    
    self.textField.delegate = self;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardDidShow:(NSNotification *)noti{
    if (keyboardVisible) {
        return;
    }
    NSDictionary *info = [noti userInfo];
    NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    
    CGRect viewFrame = self.scrollView.frame;
    viewFrame.size.height -= (keyboardSize.height);
    self.scrollView.frame = viewFrame;
    
    CGRect textFieldRect = [self.textField frame];
    [self.scrollView scrollRectToVisible:textFieldRect animated:YES];
    
    keyboardVisible = YES;
}

- (void)keyboardDidHide:(NSNotification *)noti{
    
}

-(void)viewDidAppear:(BOOL)animated
{
    [self.scrollView setContentOffset:CGPointMake(0, 100) animated:YES];
    [super viewDidAppear:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
